
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
using LPV_POS;
public partial class frmDM_NHACC : Form
{
    public string ma = "-1";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
    DataTable tbMainDS = new DataTable();
    DataTable tbMain = new DataTable();
    Boolean iloadform = false;

    #region"    frmDM_NHACC"
    public frmDM_NHACC(Boolean iChon)
    {
        InitializeComponent();
        btnChon.Visible = iChon;
    }
    #endregion

    #region"    frmDM_NHACC_Load"
    public void frmDM_NHACC_Load(object sender, EventArgs e)
    {
        btnluu.Enabled = false;
        btnxoa.Enabled = false;
        this.showcombobox();
        this.showdataDS();
        iloadform = true;
    }
    #endregion

    #region"    các hàm load dữ liệu"
    #region"    showdata"
    void showdata()
    {
        

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMain = othuvien.SP_SelectDataTable("sp_DM_NHACC",
                           new string[] { "@flag", "@OBJID" },
                           new object[] { 4, ma });

    }
    #endregion
    #region"    showdataDS"
    void showdataDS()
    {
        string[] param = new string[50];
        object[] value = new object[50];
        int ipos = 0;
        param[ipos] = "@flag";
        value[ipos++] = 4;
        
        grdDM_NHACC.AutoGenerateColumns = false;

        thuvien othuvien = new thuvien();//lớp truy xuat du lieu 
        tbMainDS = othuvien.SP_SelectDataTable("sp_DM_NHACC", param, value);
        grdDM_NHACC.DataSource = tbMainDS;
        grdDM_NHACC.Refresh();

    }
    #endregion
    #region"    showinfo"
    void showinfo(int i1edt2sch)
    {
     
            this.showdata();
        if (tbMain.Rows.Count == 0)
        {
     
            txtOBJNAME.Text = "";
            
            //dtpNGAYSINH.Value = DateTime.Now;
            
            txtDIENTHOAIBAN.Text = "";
            txtDIENTHOAIDD.Text = "";
            txtDIACHI.Text = "";
            txtNGUOILIENHE.Text = "";
            txtGHICHU.Text = "";
            return;
        }
        
        txtOBJNAME.Text = tbMain.Rows[0]["OBJNAME"] + "";
        cboMANHOMNCC.SelectedValue = tbMain.Rows[0]["MANHOMNCC"];
        
        
        dtpNGAYSINH.Text = tbMain.Rows[0]["NGAYSINH"] + "";
        txtDIENTHOAIBAN.Text = tbMain.Rows[0]["DIENTHOAIBAN"] + "";
        txtDIENTHOAIDD.Text = tbMain.Rows[0]["DIENTHOAIDD"] + "";
        txtDIACHI.Text = tbMain.Rows[0]["DIACHI"] + "";
        txtNGUOILIENHE.Text = tbMain.Rows[0]["NGUOILIENHE"] + "";
        txtGHICHU.Text = tbMain.Rows[0]["GHICHU"] + "";

    }
    #endregion
    #region"    showcombobox"
    void showcombobox()
    {

        thuvien othuvien = new thuvien();

        DataTable tb = othuvien.SP_SelectDataTable("sp_DM_NHACC",
                           new string[] { "@flag" },
                           new object[] { 5 });
        cboMANHOMNCC.DataSource = tb;

    }
    #endregion
    #endregion

    #region"    các sự kiện _Click của các nút THÊM - SỬA- TÌM KIẾM - THOÁT - tbpDanhSach"
    #region"    btnDSthem_Click"
    private void btnDSthem_Click(object sender, EventArgs e)
    {
        tbMain = tbMainDS.Copy();
        tbMain.Clear();
        ma = "-1";
        this.showinfo(1);
        this.setStatusButton(1);
    }

    #endregion

    #region"    btnDSthoat_Click"
    private void btnDSthoat_Click(object sender, EventArgs e)
    {
        //if (MessageBox.Show("Bạn muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            this.Dispose();
        }

    }
    #endregion

    #endregion

    #region"    các sự kiện _Click của các nút LƯU - XÓA - SỬA  "

    #region"    btnluu_Click"
    private void btnluu_Click(object sender, EventArgs e)
    {
        if (this.save() > 0)
        {
            MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showdataDS();
        }
    }

    #endregion
    #region"    save"
    int save()
    {
        int _iReasult = -1;
        if (this.ValidateSave())
        {
            thuvien othuvien = new thuvien();//lớp truy xuat du lieu
            
            _iReasult = othuvien.SP_Update("sp_DM_NHACC",
                 new string[] { "@flag", "@OBJID", "@OBJNAME", "@MANHOMNCC", "@NGAYSINH", "@DIENTHOAIBAN", 
                                "@DIENTHOAIDD", "@DIACHI", "@NGUOILIENHE", "@GHICHU" },
                 new object[] { 1, ma, txtOBJNAME.Text, cboMANHOMNCC.SelectedValue, dtpNGAYSINH.Value, txtDIENTHOAIBAN.Text, 
                                txtDIENTHOAIDD.Text, txtDIACHI.Text, txtNGUOILIENHE.Text, txtGHICHU.Text });
        }
        return _iReasult;
    }

    #endregion
    #region"    ValidateSave"
    Boolean ValidateSave()
    {
        return true;
    }

    #endregion
    #region"    delete"
    int delete()
    {
        thuvien othuvien = new thuvien();//lớp truy xuat du lieu    
        int _iReasult = othuvien.SP_Update("sp_DM_NHACC",
                                 new string[] { "@flag", "@OBJID" },
                                 new object[] { 3, ma, });
        return _iReasult;
    }

    #endregion
    #region"    btnxoa_Click"
    private void btnxoa_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Bạn muốn xóa NHÀ CUNG CẤP này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
            if (this.delete() > 0)
            {
                MessageBox.Show("Xóa NHÀ CUNG CẤP thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ma = "-1";
                this.showdataDS();
                this.setStatusButton(4);
            }
        }
    }

    #endregion
    #region"    sự kiên chọn trên lưới -- tblDanhSach"
   
    #region"    grdDM_NHACC_SelectionChanged"
    private void grdDM_NHACC_SelectionChanged(object sender, EventArgs e)
    {

        if (iloadform && grdDM_NHACC.CurrentRow != null)
        {
            if (grdDM_NHACC.CurrentRow.Index >= 0)
            {
                iloadform = false;
                ma = "-1";
                string _iSTT = grdDM_NHACC.Rows[grdDM_NHACC.CurrentRow.Index].Cells[0].Value + "";
                DataRow[] _dr = tbMainDS.Select("STT='" + _iSTT + "'");
                if (_dr.Length > 0)
                {
                    ma = _dr[0]["OBJID"] + "";
                    this.setStatusButton(2);
                }
                this.showinfo(1);
                iloadform = true;
            }
        }
    }
    private void grdDM_NHACC_DoubleClick(object sender, EventArgs e)
    {
        this.grdDM_NHACC_SelectionChanged(null, null);
    }
    #endregion
    #endregion
    #region "    setStatusButton"
    void setStatusButton(int i1New2Edt3Sch4Del5Sav)
    {
        btnDSthem.Enabled = i1New2Edt3Sch4Del5Sav != 1;
        btnxoa.Enabled = (i1New2Edt3Sch4Del5Sav != 1 && i1New2Edt3Sch4Del5Sav != 4 && i1New2Edt3Sch4Del5Sav != 3);
        
        btnluu.Enabled = i1New2Edt3Sch4Del5Sav != 5;
    }
    #endregion

    #region"    grdDM_NHACC_DataError"
    private void grdDM_NHACC_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion
    #region"    grd_DataError"
    private void grd_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
        string sError = e.Exception.Message;
    }
    #endregion

    private void btnChon_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }
    #endregion

    public DataRow ShowForm()
    {
        this.ShowDialog();
        if (tbMain.Rows.Count > 0)
            return tbMain.Rows[0];
        else
            return null;
    }

   

}
